// 道具相关
// noinspection JSUnusedLocalSymbols

import {MyCache} from "@ginstone/common-utils/dist/src/utils/MyCache";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {ItemBuyParam, ItemStoreParam} from "@ginstone/nga-api/dist/src/params/ItemParams";
import {ElementUtils} from "@/assets/js/ElementUtils";

export default {
    namespaced: true,
    state: {
        cache: new MyCache({limit: 60 * 30}),
    },
    mutations: {
        method(state, payload) {

        },
    },
    actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {
            const client = rootGetters["Account/getClient"]();
        },
        // 查询商店
        store: ({dispatch, commit, state, rootGetters}, {force, page, type, subTypes}) => {
            const client = rootGetters["Account/getClient"]();
            const param = new ItemStoreParam(page, type, subTypes);
            const method = () => NukeApi.itemStore(client, param)
            return state.cache.use(param, method, force)
        },
        // 我的库存
        inventory: ({dispatch, commit, state, rootGetters}, {force, page}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.itemInventory(client, page)
            return state.cache.use({type: 'inventory', userId: client.userId, page}, method, force)
        },
        // 购买道具
        buy: ({dispatch, commit, state, rootGetters}, {id, count}) => {
            const client = rootGetters["Account/getClient"]();
            return NukeApi.itemBuy(client, new ItemBuyParam(id, count)).then(message => {
                ElementUtils.notice({message})
                state.cache.clear()
            })
        },
        // 使用道具
        use: ({dispatch, commit, state, rootGetters}, param) => {
            const client = rootGetters["Account/getClient"]();
            return NukeApi.itemUse(client, param).then(message => {
                ElementUtils.notice({message})
                state.cache.clear()
            })
        },
    },
    getters: {},
}